package horario.vista;

import horario.controlador.ControladorEntrada;
import horario.controlador.ControladorJustificacion;
import horario.recursos.beans.BeanFechaHora;
import horario.recursos.beans.BeanJustificacion;
import horario.recursos.utiles.LE;
import java.awt.Color;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class justifica extends javax.swing.JDialog {

    ControladorJustificacion controladorJustificacion = new ControladorJustificacion();
    ControladorEntrada controladorEntrada = new ControladorEntrada();
    ArrayList<BeanJustificacion> listaBean = null;
    boolean insertado = false;
    int id = 0;
    Date fecha_hoy = null;
    Time hora_actual = null;

    /** Creates new form justifica */
    public justifica(java.awt.Frame parent, boolean modal, BeanFechaHora bean) {
        super(parent, modal);
        try {
            listaBean = controladorJustificacion.listarJustificacion();
        } catch (Exception ex) {
            Logger.getLogger(horario.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        txtUsuario.setText(bean.getUsuario());
        txtUsuario.setEnabled(false);
        txtaOtros.setEnabled(false);
        txtaOtros.setBackground(Color.LIGHT_GRAY);
        id = bean.getIdUsuario();
        fecha_hoy = bean.getFecha();
        hora_actual = bean.getHora();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbxMotivo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaOtros = new javax.swing.JTextArea();
        btnAceptarMotivo = new javax.swing.JButton();
        txtUsuario = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        try {
            ArrayList<String> lista = new ArrayList<String>();
            lista.add("[SELECCIONE]");
            for (BeanJustificacion x : listaBean){
                lista.add(x.getJustificacion());
            }

            String [] listaJustificacion = new String[lista.size()];
            lista.toArray(listaJustificacion);
            cbxMotivo.setModel(new javax.swing.DefaultComboBoxModel(listaJustificacion));
        } catch (Exception ex) {
            Logger.getLogger(horario.class.getName()).log(Level.SEVERE, null, ex);
        }
        cbxMotivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxMotivoActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Motivo de la tardanza de");

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        txtaOtros.setColumns(20);
        txtaOtros.setRows(5);
        jScrollPane1.setViewportView(txtaOtros);

        btnAceptarMotivo.setText("Aceptar");
        btnAceptarMotivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarMotivoActionPerformed(evt);
            }
        });

        txtUsuario.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(47, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAceptarMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(43, 43, 43))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cbxMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(74, 74, 74))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(91, 91, 91))))
            .addGroup(layout.createSequentialGroup()
                .addGap(114, 114, 114)
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(112, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(cbxMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAceptarMotivo))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbxMotivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxMotivoActionPerformed
        // TODO add your handling code here:
        if (cbxMotivo.getSelectedItem().toString().equals("Otros")) {
            txtaOtros.setEnabled(true);
            txtaOtros.setBackground(Color.WHITE);
        } else {
            txtaOtros.setEnabled(false);
            txtaOtros.setBackground(Color.LIGHT_GRAY);
            txtaOtros.setText("");
        }
    }//GEN-LAST:event_cbxMotivoActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarMotivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarMotivoActionPerformed
        // TODO add your handling code here:
        int tipo = 0;
        if (cbxMotivo.getSelectedItem().toString().equals("[SELECCIONE]")) {
            LE.mostrarError("Seleccione un motivo");
        } else {
            for (BeanJustificacion x : listaBean){
                if (x.getJustificacion().equals(cbxMotivo.getSelectedItem().toString())){
                    tipo = x.getId();
                }
            }
            if (cbxMotivo.getSelectedItem().toString().equals("Otros") && (txtaOtros.getText().equals(""))) {
                LE.mostrarError("Especifique el motivo de la tardanza");
            } else {
                try {
                    insertado = controladorEntrada.insertarFechaHora(true, id, fecha_hoy, hora_actual, false, tipo, txtaOtros.getText());
                } catch (Exception ex) {
                    Logger.getLogger(justifica.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (insertado) {
                    LE.mostrarInformacion("Se registro el motivo de la tardanza");
                    dispose();
                } else {
                    LE.mostrarInformacion("No se pudo registrar el motivo de la tardanza");
                }
                
            }
        }

}//GEN-LAST:event_btnAceptarMotivoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                justifica dialog = new justifica(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptarMotivo;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cbxMotivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtUsuario;
    private javax.swing.JTextArea txtaOtros;
    // End of variables declaration//GEN-END:variables
}
